﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Screenshot
{
    internal class Win32API
    {
        internal struct SIZE
        {
            internal int cx;
            internal int cy;
        }
        internal const int SRCCOPY = 13369376;
        internal const int SM_CXSCREEN = 0;
        internal const int SM_CYSCREEN = 1;

        [DllImport( "gdi32.dll", EntryPoint = "DeleteDC" )]
        internal static extern IntPtr DeleteDC( IntPtr hDc );

        [DllImport( "gdi32.dll", EntryPoint = "DeleteObject" )]
        internal static extern IntPtr DeleteObject( IntPtr hDc );

        [DllImport( "gdi32.dll", EntryPoint = "BitBlt" )]
        internal static extern bool BitBlt( IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int RasterOp );

        [DllImport( "gdi32.dll", EntryPoint = "CreateCompatibleBitmap" )]
        internal static extern IntPtr CreateCompatibleBitmap( IntPtr hdc, int nWidth, int nHeight );

        [DllImport( "gdi32.dll", EntryPoint = "CreateCompatibleDC" )]
        internal static extern IntPtr CreateCompatibleDC( IntPtr hdc );

        [DllImport( "gdi32.dll", EntryPoint = "SelectObject" )]
        internal static extern IntPtr SelectObject( IntPtr hdc, IntPtr bmp );

        [DllImport( "user32.dll", EntryPoint = "GetDesktopWindow" )]
        internal static extern IntPtr GetDesktopWindow();

        [DllImport( "user32.dll", EntryPoint = "GetDC" )]
        internal static extern IntPtr GetDC( IntPtr ptr );

        [DllImport( "user32.dll", EntryPoint = "GetSystemMetrics" )]
        internal static extern int GetSystemMetrics( int abc );

        [DllImport( "user32.dll", EntryPoint = "GetWindowDC" )]
        internal static extern IntPtr GetWindowDC( IntPtr ptr );

        [DllImport( "user32.dll", EntryPoint = "ReleaseDC" )]
        internal static extern IntPtr ReleaseDC( IntPtr hWnd, IntPtr hDc );

        [DllImport( "User32.dll" )]
        internal static extern IntPtr FindWindow( String lpClassName, String lpWindowName );

        [StructLayout( LayoutKind.Sequential )]
        internal struct RECT
        {
            internal int left;
            internal int top;
            internal int right;
            internal int bottom;
        }

        [DllImport( "user32.dll" )]
        internal static extern bool GetClientRect( IntPtr handle, out RECT lpRect );

        internal static bool IsValidHandle( IntPtr handle )
        {
            return handle != IntPtr.Zero;
        }

        internal static bool IsValidWindowHandle( IntPtr handle )
        {
            return IsValidHandle( handle ) && IsWindow( handle );
        }

        [DllImport( "user32.dll" )]
        internal extern static bool IsWindow( IntPtr handle );

        [DllImport( "user32.dll" )]
        internal static extern bool IsIconic( IntPtr handle );


        internal enum WindowShowStyle : uint
        {
            /// <summary>Hides the window and activates another window.</summary>
            /// <remarks>See SW_HIDE</remarks>
            Hide = 0,
            /// <summary>Activates and displays a window. If the window is minimized 
            /// or maximized, the system restores it to its original size and 
            /// position. An application should specify this flag when displaying 
            /// the window for the first time.</summary>
            /// <remarks>See SW_SHOWNORMAL</remarks>
            ShowNormal = 1,
            /// <summary>Activates the window and displays it as a minimized window.</summary>
            /// <remarks>See SW_SHOWMINIMIZED</remarks>
            ShowMinimized = 2,
            /// <summary>Activates the window and displays it as a maximized window.</summary>
            /// <remarks>See SW_SHOWMAXIMIZED</remarks>
            ShowMaximized = 3,
            /// <summary>Maximizes the specified window.</summary>
            /// <remarks>See SW_MAXIMIZE</remarks>
            Maximize = 3,
            /// <summary>Displays a window in its most recent size and position. 
            /// This value is similar to "ShowNormal", except the window is not 
            /// actived.</summary>
            /// <remarks>See SW_SHOWNOACTIVATE</remarks>
            ShowNormalNoActivate = 4,
            /// <summary>Activates the window and displays it in its current size 
            /// and position.</summary>
            /// <remarks>See SW_SHOW</remarks>
            Show = 5,
            /// <summary>Minimizes the specified window and activates the next 
            /// top-level window in the Z order.</summary>
            /// <remarks>See SW_MINIMIZE</remarks>
            Minimize = 6,
            /// <summary>Displays the window as a minimized window. This value is 
            /// similar to "ShowMinimized", except the window is not activated.</summary>
            /// <remarks>See SW_SHOWMINNOACTIVE</remarks>
            ShowMinNoActivate = 7,
            /// <summary>Displays the window in its current size and position. This 
            /// value is similar to "Show", except the window is not activated.</summary>
            /// <remarks>See SW_SHOWNA</remarks>
            ShowNoActivate = 8,
            /// <summary>Activates and displays the window. If the window is 
            /// minimized or maximized, the system restores it to its original size 
            /// and position. An application should specify this flag when restoring 
            /// a minimized window.</summary>
            /// <remarks>See SW_RESTORE</remarks>
            Restore = 9,
            /// <summary>Sets the show state based on the SW_ value specified in the 
            /// STARTUPINFO structure passed to the CreateProcess function by the 
            /// program that started the application.</summary>
            /// <remarks>See SW_SHOWDEFAULT</remarks>
            ShowDefault = 10,
            /// <summary>Windows 2000/XP: Minimizes a window, even if the thread 
            /// that owns the window is hung. This flag should only be used when 
            /// minimizing windows from a different thread.</summary>
            /// <remarks>See SW_FORCEMINIMIZE</remarks>
            ForceMinimized = 11
        }

        [DllImport( "user32.dll" )]
        internal static extern bool ShowWindow( IntPtr handle, WindowShowStyle nCmdShow );
    }
}
